package com.jialiang.c;

import com.amazonaws.services.s3.model.PutObjectResult;
import com.jialiang.oss.client.OssClient;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.io.InputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author jialiang 2024/10/12 15:20
 */
@RestController
@RequestMapping("/test")
@RequiredArgsConstructor
public class TestController {

    private static final Logger logger = LoggerFactory.getLogger(TestController.class);

    private final OssClient ossClient;

    @GetMapping("")
    public String test() {
        return "hello world";
    }

    @GetMapping("/createBucket")
    private void testOss() {
        ossClient.createBucket("test2");
    }

    @GetMapping("/listBuckets")
    public void listBuckets() {
        String bucketName = "test2";
        String objectName = "login.jpg"; // 修改对象名称

        try (InputStream stream = TestController.class.getResourceAsStream("/static/images/login.jpg")) {
            if (stream == null) {
                logger.error("无法找到资源文件: /static/images/logo.png");
                return;
            }

            long size = stream.available();
            String contentType = "image/jpg";

            ossClient.putObject(bucketName, objectName, stream, size, contentType);
            logger.info("对象 {} 已成功上传到桶 {}", objectName, bucketName);
        } catch (IOException e) {
            logger.error("上传对象时发生错误: {}", e.getMessage(), e);
        }
    }




}
